package com.gxa.saas.controller;

import com.gxa.saas.pojo.GoodsDetailed;
import com.gxa.saas.pojo.GoodsXq;
import com.gxa.saas.pojo.Order;
import com.gxa.saas.pojo.Storage;
import com.gxa.saas.service.OrderAddService;
import com.gxa.saas.service.OrderService;
import com.gxa.saas.service.StorageService;
import com.gxa.saas.utils.JsonResult;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/salesAdd")
@CrossOrigin
public class SalesAddController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderAddService orderAddService;

    @Autowired
    StorageService storageService;
//    @Autowired
//    StorageService storageService;
    @ApiOperation(value = "添加订单",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order",value = "添加内容",paramType = "query"),

    })
    @PostMapping("/add")
    public Map insert(@RequestBody Order order){

        Integer insert = orderService.insert(order);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("添加成功");
        return jsonResult.getMap();
    }




    @ApiOperation(value = "搜索查询库存明细",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "模糊查询",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),

    })
    @PostMapping("/selectSorage")
    public Map selectSorage(String name, Integer page ,Integer size){

        List<Storage> list = orderAddService.selectByName(name, page, size);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(list);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "进页面展示库存明细",produces = "query")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),

    })
    @PostMapping("/selectByList")
    public Map selectByList(Integer page ,Integer size){

        List<Storage> list = orderAddService.selectByList(page, size);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(list);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "商品明细分页展示",produces = "query")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),

    })
    @PostMapping("/selectByGoodsXqList")
    public Map selectByGoodsXqList(Integer page ,Integer size){

        List<GoodsXq> goodsXqs = orderAddService.selectByGoodsXq(page, size);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(goodsXqs);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "搜索结果添加接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "搜索结果添加对应的商品id",dataType = "int",paramType = "query"),

    })
    @PostMapping("/addgoods")
    public Map insertgoods( Integer id){

        Storage storage = orderAddService.selectById(id);
        StringBuilder big=new StringBuilder();
        StringBuilder small=new StringBuilder();
        GoodsXq goodsXq = new GoodsXq();

        String bigUnit=storage.getBigUnit();
        String bigPrice= String.valueOf(storage.getBigPrice());

        big.append(bigPrice);
        big.append("/");
        big.append(bigUnit);
        String smallUnit=storage.getSmallUnit();
        String smallPrice= String.valueOf(storage.getSmallPrice());
        small.append(smallPrice);
        small.append("/");
        small.append(smallUnit);

        goodsXq.setName(storage.getName());
        goodsXq.setBigUnit(big.toString());
        goodsXq.setSmallUnit(small.toString());
        Double bigPrice1 = storage.getBigPrice();
        Double smallPrice1 = storage.getSmallPrice();
        goodsXq.setBigCount(1);
        goodsXq.setSmallCount(1);
        goodsXq.setGoodsType("正常");

        goodsXq.setMoney(bigPrice1*1+smallPrice1*1);
        goodsXq.setProtectedDate(new Date());

        Integer insert = orderAddService.insert(goodsXq);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("添加成功");
        return jsonResult.getMap();
    }
}
